import { CacheKeys } from "@/constant/CacheKeys";
import { GenerateResult, Uri } from "@/constant/Constants";
import { SdRequest } from "@/protocol/request/SdRequest";
import ProtocolResponse from "@/protocol/response/ProtocolResponse";
import { SdResponse } from "@/protocol/response/SdResponse";
import Http from "@/utils/Http";
import UniUtil from "@/utils/UniUtil";
import Util from "@/utils/Util";

export default class SdService {
    public static async queryTags() {
        return await Http.request(Uri.tags_query, null, new Array<SdResponse.SdModelTag>(), false, false);
    }

    public static async queryModels() {
        return await Http.request(Uri.models_query, null, new Array<SdResponse.SdModelItem>());
    }

    public static async queryModelImages(request: SdRequest.SdModelQuery) {
        return await Http.request(Uri.models_image_query, request, new Array<SdResponse.SdModelPromptItem>());
    }

    public static async queryPrompt() {
        return await Http.request(Uri.prompt_query, null, new SdResponse.Prompt());
    }

    public static async queryPromptTags() {
        return await Http.request(Uri.prompt_tag_query, null, new Array<SdResponse.SmartTag>());
    }

    public static async createImage(request: SdRequest.CreateImage){
        return await Http.request(Uri.image_create, request, new ProtocolResponse<SdResponse.CreateImage>(), false, false,false);
    }


    public static async queryImage(request: SdRequest.QueryImage) {
        return await Http.request(Uri.image_query, request, new SdResponse.CreateImage(), false, false,false);
    }

    public static async generateImage(that: any, request: SdRequest.CreateImage, navigatePath: string):Promise<ProtocolResponse<SdResponse.CreateImage>> {
        let res = await SdService.createImage(request);
        if (null != res && res.isSuccess()) {
            let result = res.getResult();
            that.showGenerateLoading = true;
            let queryRequest = new SdRequest.QueryImage();
            queryRequest.id = result.id;
            let status = GenerateResult.PROCESSING;
            while (status != GenerateResult.SUCCESS && status != GenerateResult.FAIL) {
                await Util.sleep(150);
                let queryResult = await SdService.queryImage(queryRequest);
                status = queryResult.status;
                that.generatePercent += (queryResult.progress - that.generatePercent / 100) * 100;
                if (that.generatePercent > 100) {
                    that.generatePercent = 100;
                }
                request.resultUrl = queryResult.resultUrl;
            }
            if (status == GenerateResult.FAIL) {
                that.generateTitle = "图片生成失败";
                that.showGenerateLoading = false;
                return res;
            } else {
                that.generateTitle = "图片生成成功";
            }
            UniUtil.setStorage(CacheKeys.RESULT, request);

            setTimeout(() => {
                that.showGenerateLoading = false;
                if (!Util.isEmpty(navigatePath)) {
                    uni.navigateTo({ url: navigatePath });
                }
            }, 1500);

        } 
        return res;
    }


}